package com.zld.smarket.service.serviceImpl;

import com.zld.smarket.dao.GoodsMapper;
import com.zld.smarket.dao.GoodsTypeMapper;
import com.zld.smarket.dao.SaleMapper;
import com.zld.smarket.dto.LossList;
import com.zld.smarket.dto.SaleList;
import com.zld.smarket.dto.TotalSaleData;
import com.zld.smarket.dto.TypeRate;
import com.zld.smarket.pojo.GoodsType;
import com.zld.smarket.pojo.Sale;
import com.zld.smarket.result.ResponseData;
import com.zld.smarket.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SaleServiceImpl implements SaleService {
    @Autowired
    private SaleMapper saleMapper;
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public ResponseData findList(Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<SaleList> saleLists = saleMapper.selectAllAndPage(page,limit);
        Long count = saleMapper.countSale();
        return new ResponseData(saleLists,count);
    }

    @Override
    public Long selectbyno(Long i) {
        return saleMapper.countSalebyno(i);
    }

    @Override
    public Integer addsale(Sale sale) {
        int j=0;
        j=saleMapper.insert(sale);
        return j;
    }

    @Override
    public List<TotalSaleData> weeksale() {
        List<TotalSaleData> weeksale = new ArrayList<TotalSaleData>();
        TotalSaleData a ;
        TotalSaleData b =new TotalSaleData();
        for(int i=6;i>=0;i--){
            a =saleMapper.selectsale(i);
            if (a==null){

                b.setSalesVolume(0L);
                b.setProfit(0L);
                weeksale.add(b);

            }else {
                weeksale.add(a);
            }
            }

        return weeksale;
    }

    @Override
    public List<Long> salenum(String s) {
        List<Long> salenum = new ArrayList<Long>();

        for(int i=6;i>=0;i--){
            Long a = saleMapper.selectsalenum(i,s);
            if(a==null){
                salenum.add(0L);
            }else{
                salenum.add(a);
            }


        }

        return salenum;
    }

    @Override
    public TotalSaleData weektotalsale() {
        return saleMapper.weektotalsale();
    }

    @Override
    public List<TypeRate> typerate() {
        List<TypeRate> typeRateList =new ArrayList<TypeRate>();
        List<Long> typelist =goodsTypeMapper.gettypeno();
        TypeRate typeRate;
        for(int i=0 ;i<typelist.size();i++){
            typeRate = saleMapper.typerate(typelist.get(i));
            if(typeRate!=null){
                typeRateList.add(typeRate);
            }
            typeRate =null;
        }

      return typeRateList;

    }

    @Override
    public ResponseData search(String s, Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 100;
        }

        List<SaleList> saleLists =  saleMapper.search(s,page,limit);
        Long count = saleMapper.countSale();
        return new ResponseData(saleLists,count);
    }

    @Override
    public TotalSaleData monthtotalsale() {
        return saleMapper.monthtotalsale();
    }

    @Override
    public List<TypeRate> salenumrate() {
        List<TypeRate> typeRateList =new ArrayList<TypeRate>();
        List<Long> goodsnolist =goodsMapper.getgoodsno();
        TypeRate typeRate;
        for(int i=0 ;i<goodsnolist.size();i++){
            typeRate = saleMapper.salenumrate(goodsnolist.get(i));
            if(typeRate!=null){
                typeRateList.add(typeRate);
            }
            typeRate =null;
        }

        return typeRateList;
    }
}
